<?


    function getMatrices($xml_file) {
        global $matrizes;
        $objeto = new DomDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("matrices");
        if ($filho->length > 0) {
            $temp = $filho->item(0)->getelementsbytagname("matrix");
            $j = 0;
            for($i = 0; $i < $temp->length; $i++) {
                $conteudo = $temp->item($i)->getelementsbytagname("dim");
                if ($conteudo->length > 0) {
                $matrizes[$j] = $temp->item($i);
                $j++;
                }
            }
            return $matrizes;
        }
        else return null;
    }

    function getDim($indice) {
        global $matrizes, $dim;
        $dimensao = $matrizes[$indice]->getelementsbytagname("dim");
        $dim = $dimensao->item(0)->nodeValue;
        return $dim;
    }

    function getDesc($indice) {
        global $matrizes, $desc;
        $descricao = $matrizes[$indice]->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $matrizes, $com;
        $comentario = $matrizes[$indice]->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $matrizes, $ref;
        $referencia = $matrizes[$indice]->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function getMatrix($indice) {
        global $matrizes, $A;
        $linhas = $matrizes[$indice]->getelementsbytagname("row");
        for($i = 0; $i < $linhas->length; $i++) {
            $x = $linhas->item($i)->getelementsbytagname("x");
            for($j = 0; $j < $x->length; $j++) {
                $A[$i][$j] = $x->item($j)->nodeValue;
            }
        }
        return $A;
    }


    function createMenu() {
        global $matrizes,$msg;
        for($i = 0; $i < sizeof($matrizes); $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            echo  $msg->get('XML_EXIBE_MATRIZ_MSG01'); echo getDim($i);
            echo "</u></a><p>";
        }
        return;
    }


?>

<? if (empty($xml_file) && empty($id_bib)) $id_bib = "bib_demo.xml";
   if ( (!isset($indice)) || ($indice == null) ) { ?>
<p>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=transposta.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

	  <? //$document_root = 'c:/adaptweb/adaptweb/html/anaweb';
      ?>


        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib($xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getMatrices($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
	    } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
	    }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
        </form>

<? } else { ?>
<p>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
               document.myform.Calcular.value = 'sim';
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=transposta.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getMatrices($xml_file); ?>
        <? $dim = getDim($indice); ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? $A = getMatrix($indice); ?>

        <? verificaBib($xml_file); ?>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MATRIZ_MSG02'); ?></b><? echo $dim ?></font>
        <input type="hidden" name=dim value="<? echo $dim ?>">
        <p>
        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MATRIZ_MSG03'); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        <p>
		<p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MATRIZ_MSG04'); ?></b></font>
		<p>
		<?
		//<table bordercolor="#000000" frame="border" rules="all" style="border:3px none">
		?>
		<table border=0>
		<? for ($i=0; $i<$dim; $i++) {	?>
            <tr>
	   	<?	for ($j=0; $j<$dim; $j++) { ?>
			    <td align=center valign=center width=20> <? echo $A[$i][$j] ?> </td>
			    <input type="hidden" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo $A[$i][$j] ?>">
            <? } /* end for j */?>
		</tr>
		<? } //end for i ?>
        </table>
        <p>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>


        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
	<input type="hidden" name="navegacao" value="sim">
        <input type="hidden" name="Calcular" >
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        </form>
<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=transposta.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "<br><br>".$msg->get('Outros_Exemplos')."<br>";
    $objeto = new DomDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("matrices");
	if ($filho->length > 0)
	{
		$temp = $filho->item(0)->getelementsbytagname("matrix");
		$j = 0;
		for($i = 0; $i < $temp->length; $i++)
		{
			$conteudo = $temp->item($i)->getelementsbytagname("dim");
			if ($conteudo->length > 0)
			{
				if ($j != 0) echo " | ";
				if ($j == $indice) echo "<b>";
				echo "<a href=\"javascript:criarLinks($j)\" title=\"Dimens&atilde;o ".$conteudo->item(0)->nodeValue."\"><u>".($j+1)."</u></a>";
				if ($j == $indice) echo "</b>";
				$j++;
			}
		}
	}
?>
<? } ?>
